package com.download.zhuaba.Controller;

import com.download.zhuaba.Dao.UserRepository;
import com.download.zhuaba.Entity.JsonResult;
import com.download.zhuaba.Entity.User;
import com.download.zhuaba.Service.WelcomeService;
import com.download.zhuaba.Utils.enCodeByBase64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WelcomeController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/welcome.html")
    public String login(Model model, HttpServletRequest request, HttpSession session){
        if(request.getSession().getAttribute("loginUser")!=null){
            String str = WelcomeService.getHtml();
            session.setAttribute("flag", "1");//跳转地址不带上u2参数
            session.setAttribute("login","1");
            model.addAttribute("htmlCode",str);
            return "welcome";
        }
       else{
            session.setAttribute("flag", "1");//跳转地址不带上u2参数
            session.setAttribute("login","0");
            return "login";
        }
    }
    @ResponseBody
    @RequestMapping(value = "/welcome/getPerson", method = RequestMethod.POST)
    public JsonResult<Map<String, String>> getUserAmount(HttpServletRequest request) {
        int userId =Integer.valueOf(request.getSession().getAttribute("loginUser").toString());
        List<User> list = new ArrayList<User>();
        User user =new User();
        list = userRepository.findAll();

        JsonResult<Map<String, String>> result = new JsonResult<>();
        try {
            Map<String, String> amountMap = new HashMap<>();
            if (list != null) {
                for (User user1 : list) {
                    if (user1.getId()==userId) {
                        amountMap.put("username",user1.getUsername());
                        amountMap.put("email",user1.getEmail());
                    }
                }
            }
            result.setData(amountMap);
            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
            result.setMessage("person");

        } catch (Exception e) {
        }
        return result;
    }
//    @RequestMapping(value = "/PersonController", method = RequestMethod.POST)
//    public String registerAdmin(HttpSession session,@RequestParam("Username") String Username, @RequestParam("Password") String Password, @RequestParam("Email") String Email,HttpServletRequest request, RedirectAttributes attr) {
//        int userId =Integer.valueOf(request.getSession().getAttribute("loginUser").toString());
//        User user = new User();
//        user.setId(userId);
//        user.setUsername(Username);
//        user.setEmail(Email);
//        user.setPassword(Password);
//        userRepository.save(user);
//        session.setAttribute("person", "1");//跳转地址不带上u2参数
//        return "redirect:http://localhost:8080/Person.html";
//    }
    @GetMapping("/Person.html")
    public String person(HttpSession session,HttpServletRequest request) {
        if(request.getSession().getAttribute("loginUser")!=null) {
            session.setAttribute("person","0");
            return "Person";
        }
        else{
            session.setAttribute("flag", "1");//跳转地址不带上u2参数
            session.setAttribute("login","0");
            return "login";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/person/getPerson", method = RequestMethod.POST)
    public JsonResult<Map<String, Integer>> getPersonAmount(HttpSession session,HttpServletRequest request,@RequestParam("Username") String Username, @RequestParam("Password") String Password, @RequestParam("Email") String Email) {
        JsonResult<Map<String, Integer>> result = new JsonResult<>();
        try {
            int userId =Integer.valueOf(request.getSession().getAttribute("loginUser").toString());
            User user = new User();
            user.setId(userId);
            user.setUsername(Username);
            user.setEmail(Email);
            enCodeByBase64 encode=new enCodeByBase64();
            Password = encode.enCode(Password);
            user.setPassword(Password);
            userRepository.save(user);
            session.setAttribute("person", "1");//跳转地址不带上u2参数
            Map<String, Integer> amountMap = new HashMap<>();
            if (request.getSession().getAttribute("person") == "1") {
                amountMap.put("person1", 1);
            } else {
                amountMap.put("person1", 0);
            }
            result.setData(amountMap);
            //这里举个例子，如果觉得setData穿的信息不够，还可以用setMessage方法多传一个字符串过去
            result.setMessage("person");

        } catch (Exception e) {
        }
        return result;
    }
}
